# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##

from ..packlib import info as CSI

class GameCategories(object):
    """
    Game categories. - Wrapper for the cardset CSI.TYPE_xxx.
    """
    __slots__ = []

    def __getattr__(self, name):
        name = 'TYPE_{0}'.format(name)
        if hasattr(CSI, name):
            return getattr(CSI, name)
        else:
            raise AttributeError


GC = GameCategories()


class GameTypes(object):
    """
    Game types.
      - Basic types enumerated from 0 - 40 as index of value.
      - Extended types bit shift left from 10 - 22 as index + 10 of value.
    """
    # basic bit: single main type
    _bit_ = ('ONE_DECK', 'TWO_DECK', 'THREE_DECK', 'FOUR_DECK',
             'BAKERS_DOZEN', 'BELEAGUERED_CASTLE', 'CANFIELD',
             'DASHAVATARA_GANJIFA', 'FAN_TYPE', 'FORTY_THIEVES', 'FREECELL',
             'GOLF', 'GYPSY', 'HANAFUDA', 'HEXADECK', 'KLONDIKE', 'MAHJONGG',
             'MATRIX', 'MEMORY', 'MONTANA', 'MUGHAL_GANJIFA', 'NAPOLEON',
             'NAVAGRAHA_GANJIFA', 'NUMERICA', 'PAIRING', 'POKER_TYPE',
             'PUZZLE_TYPE', 'RAGLAN', 'ROW_TYPE', 'SIMPLE_TYPE', 'SPIDER',
             'TAROCK', 'TERRACE', 'YUKON', 'SHISEN_SHO', '', '', '', '', '',
             'CUSTOM',)

    # extended bit: multiple values extending main type
    _xbit_ = ('', '', 'BETA', 'CHILDREN', 'CONTRIB', 'HIDDEN', 'OPEN',
              'ORIGINAL', 'POPULAR', 'RELAXED', 'SCORE', 'SEPARATE_DECKS',
              'XORIGINAL', 'GNOME', 'KDE', 'XPAT', 'ATARI')

    def __getattr__(self, name):
        if name in self._bit_:
            return self._bit_.index(name)
        elif name in self._xbit_:
            return 1 << (self._xbit_.index(name) + 10)
        else:
            raise AttributeError

    def getFlags(self, value):
        return value & ~1023

    def getType(self, value):
        return value & 1023

    def hasFlags(self, value, flag):
        return self.getflags(value) & flag

GT = GameTypes()


class SL(object):
    """
    Skill level.
    """

    LUCK = 1
    MOSTLY_LUCK = 2
    BALANCED = 3
    MOSTLY_SKILL = 4
    SKILL = 5

def category_convert(gtype):
    return {GT.HANAFUDA: GC.HANAFUDA,
            GT.TAROCK: GC.TAROCK,
            GT.MAHJONGG: GC.MAHJONGG,
            GT.HEXADECK: GC.HEXADECK,
            GT.MUGHAL_GANJIFA: GC.MUGHAL_GANJIFA,
            GT.NAVAGRAHA_GANJIFA: GC.NAVAGRAHA_GANJIFA,
            GT.DASHAVATARA_GANJIFA: GC.DASHAVATARA_GANJIFA
            }.get(gtype, GC.FRENCH)